#include "common/common.h"


int recv_and_print(int fd){
    char buf[512];
    int ret = recv(fd, buf, sizeof(buf), 0);
    if(ret < 0){
        close(fd);
        return -1;
    }
    printf("%s", buf);
    return ret;
}

int main(int argc, char** argv){
    if(argc != 3){
        fprintf(stderr, "Usage : %s ip port!\n", argv[0]);
        exit(1);
    }

    char ip[20] = {0};
    int port, fd, stu_port;
    strcpy(ip, argv[1]);
    port = atoi(argv[2]);

    if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket()");
        exit(1);
    }

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = inet_addr(ip);

    if(connect(fd, (struct sockaddr*)&server, sizeof(server)) < 0){
        perror("connect()");
        exit(1);
    }

    if(recv_and_print(fd) < 0){
        exit(1);
    }
    scanf("%d", &stu_port);
    send(fd, &stu_port, sizeof(int), 0);
    if(recv_and_print(fd) < 0){
        exit(1);
    }
    if(recv_and_print(fd) < 0){
        exit(1);
    }
    if(recv_and_print(fd) < 0){
        exit(1);
    }
    return 0;
}